package com.example.jn_dxs.Base;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.jn_dxs.API.api;
import com.example.jn_dxs.API.apiService;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BaseViewModel extends ViewModel {
    public MutableLiveData<String> msg = new MutableLiveData<>();
    public MutableLiveData<JSONObject> dataObject = new MutableLiveData<>();
    public MutableLiveData<JSONArray> dataArray = new MutableLiveData<>();
    public MutableLiveData<Integer> loadingStatus = new MutableLiveData<>();
    protected apiService apiService   = api.createService();

    protected void ObserverArray(Observable<JSONObject> observable) {
        loadingStatus.postValue(1);
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<JSONObject>() {
            @Override
            public void onNext(JSONObject object) {
                super.onNext(object);
                loadingStatus.postValue(2);
            }
        });
    }
    protected void ObserverObject(Observable<JSONObject> observable) {
        loadingStatus.postValue(1);
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<JSONObject>() {
            @Override
            public void onNext(JSONObject object) {
                loadingStatus.postValue(2);
                super.onNext(object);
                if (object.getIntValue("code") == 0) {
                    msg.setValue(object.getString("msg"));
                } else {
                    dataObject.setValue(object.getJSONObject("data"));
                }
            }
        });
    }
}
